<?php
class cart extends CI_Controller{
	
	function __construct(){
	parent::__construct();
	$this->load->model('cart_model');
	}
	function view_cart($id_user){
		$data['produk'] = $this->cart_model->get_produk_cart($id_user);
		$id = $this->session->userdata('id_user');
		$data['cart'] = $this->cart_model->cart($id);
		$this->load->view('cart',$data);
	}
	
	function insert_cart($id_produk, $id_cart){
		$id_user = $this->session->userdata('id_user');
		$this->cart_model->insert_cart($id_produk, $id_cart);
		redirect('cart/view_cart/'.$id_user);
	}
	
	function hapus_cart($id_item){
		$id_user = $this->session->userdata('id_user');
		$this->cart_model->delete_cart($id_item);
		redirect('cart/view_cart/'.$id_user);
	}
	
	function m_cart($id_user){
		$this->cart_model->make_cart($id_user);
		redirect('produk');
		
	}
	
	function cekout($id_cart){ 
	$id_user = $this->session->userdata('id_user');
	$stok = $this->input->post('qty'); 
	$id_item = $this->input->post('id_item'); 
	$id_produk = $this->input->post('id'); 
	//$stok_baru = $this->input->post('stok_baru'); 
		for($i=0; $i < count($stok); $i++){ 
		$this->cart_model->insertjml($id_item[$i], $stok[$i]);
		$harga = $this->cart_model->total_harga($id_item[$i]);
		$total += $harga->total;
		
		$nilai = $this->cart_model->select_stok($id_produk[$i]); 
		$hasil = $nilai - $stok[$i]; 
		$this->cart_model->update_stok($id_produk[$i], $hasil); 
		
		}
	$this->cart_model->update_payment($id_cart, $total);
	
	$data['invoice'] = $this->cart_model->invoice($id_user)->result_array();
	$this->load->view('invoice', $data);
	//redirect('cart/view_cart/'.$id_user);
	}
	
	function cek(){
	$this->load->view('invoice');
	}
	
}